package com.lemon.smartsafety.view.radar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.lemon.smartsafety.R;

/**
 * @author crazychen
 * @version 1.0
 * @date 2015/12/2
 */
public class RadarView extends View {
    private int count = 8;                //数据个数
    private int level = 6;
    private float angle = (float) (Math.PI * 2 / count);
    private float radius;                   //网格最大半径  
    private int centerX;                  //中心X  
    private int centerY;                  //中心Y  
    private String[] titles = {"人员履职率", "检查项目达标率", "消防设置完好率", "安全管理及时率", "基本信息完善率", "户籍化上报率", "消控中心在岗率", "学习培训覆盖率"};
    private String[] levelNum = {"", "20", "40", "60", "80", "100"};
    private double[] data1 = {0, 0, 0, 0, 0, 0, 0, 0}; //各维度分值
    private double[] data2 = {0, 0, 0, 0, 0, 0, 0, 0}; //各维度分值
    private float maxValue = 100;             //数据最大值
    private Paint mainPaint;                //雷达区画笔
    private Paint valuePaintOne;               //数据区画笔 One
    private Paint valuePaintTwo;               //数据区画笔 Two
    private Paint textPaint;                //文本画笔
    private Paint numPaint;                //数字画笔
    private Paint bgPaint;               //背景区画笔


    private float angleInit = (float) (Math.PI * 2 / 4);

    //实时值
    private float angleNow = 0;
    //适配手机的1dp尺寸
    private int size = 0;
    //是否显示区域Two
    private boolean isShowTwo = true;
    //数据区画笔颜色 One
    private int colorOne = -1;
    //数据区画笔颜色 Two
    private int colorTwo = -1;
    //标题画笔颜色
    private int colorTitle = -1;
    //数字画笔颜色
    private int colorNum = -1;

    //是否是绘制细线（true为绘制区域）
    private boolean isLine = false;


    public RadarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public RadarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadarView(Context context) {
        super(context);
        init();
    }


    //初始化  
    private void init() {
        //count = Math.min(data.length, titles.length);
        angle = (float) (Math.PI * 2 / count);

        mainPaint = new Paint();
        mainPaint.setAntiAlias(true);
        //mainPaint.setColor(Color.GRAY);
        mainPaint.setColor(Color.WHITE);
        mainPaint.setStyle(Paint.Style.STROKE);

        valuePaintOne = new Paint();
        valuePaintOne.setAntiAlias(true);
        if (colorOne != -1) {
            valuePaintOne.setColor(colorOne);
        } else {
            valuePaintOne.setColor(getResources().getColor(R.color.color2));
        }
        valuePaintOne.setStyle(Paint.Style.FILL_AND_STROKE);

        valuePaintTwo = new Paint();
        valuePaintTwo.setAntiAlias(true);
        if (colorTwo != -1) {
            valuePaintTwo.setColor(colorTwo);
        } else {
            valuePaintTwo.setColor(getResources().getColor(R.color.color3));
        }
        valuePaintTwo.setStyle(Paint.Style.FILL_AND_STROKE);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(getResources().getColor(R.color.color1));
        bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        textPaint = new Paint();
        size = (int) getResources().getDimension(R.dimen.dp_1);
        textPaint.setTextSize(19 * size / 2);
        textPaint.setStyle(Paint.Style.FILL);
        if (colorTitle != -1) {
            textPaint.setColor(colorTitle);
        } else {
            textPaint.setColor(Color.BLACK);
        }
        textPaint.setColor(Color.BLACK);

        numPaint = new Paint();
        numPaint.setTextSize(19 * size / 3);
        numPaint.setStyle(Paint.Style.FILL);
        if (colorNum != -1) {
            numPaint.setColor(colorNum);
        } else {
            numPaint.setColor(Color.BLACK);
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        radius = Math.min(h, w) / 2 * 0.65f;
        centerX = w / 2;
        centerY = h / 2;
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        drawPolygon(canvas);
        drawLines(canvas);
        drawText(canvas);
        drawRegionOne(canvas);
        if (isShowTwo) {
            drawRegionTwo(canvas);
        }
    }

    /**
     * 绘制正多边形
     */
    private void drawPolygon(Canvas canvas) {

        Path path = new Path();
        float r = radius / (level - 1);


        for (int i = level-1; i > 0; i--) {

            float[] start = new float[2];
            float[] befor = new float[2];
            float curR = r * i;
            path.reset();
            //Log.e("count", "count:" + count);
            for (int j = 0; j < count; j++) {
                angleNow = angle * j - angleInit;
                if (j == 0) {
                    float x = (float) (centerX + curR * Math.cos(angleNow));
                    float y = (float) (centerY + curR * Math.sin(angleNow));
                    path.moveTo(x, y);
                    canvas.drawText(levelNum[i], x + 2, y - 2, numPaint);
                    start[0] = x;
                    start[1] = y;
                } else {
                    float x = (float) (centerX + curR * Math.cos(angleNow));
                    float y = (float) (centerY + curR * Math.sin(angleNow));
                    path.lineTo(x, y);
                    if (j == 1) {
                        canvas.drawLine(start[0], start[1], x, y, mainPaint);
                    } else if (j == (count-1)) {
                        canvas.drawLine(befor[0], befor[1], x, y, mainPaint);
                        canvas.drawLine( x, y, start[0], start[1],mainPaint);
                    } else {
                        canvas.drawLine(befor[0], befor[1], x, y, mainPaint);
                    }
                    befor[0] = x;
                    befor[1] = y;
                }
            }
            path.close();
            //canvas.drawPath(path, mainPaint);
            if (i == (level-1) ) {
                bgPaint.setStyle(Paint.Style.STROKE);
                canvas.drawPath(path, bgPaint);
                bgPaint.setAlpha(127);
                //绘制填充区域
                bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                canvas.drawPath(path, bgPaint);
            }

        }


    }

    /**
     * 绘制直线
     */
    private void drawLines(Canvas canvas) {
        Path path = new Path();
        for (int i = 0; i < count; i++) {
            angleNow = angle * i - angleInit;
            path.reset();
            path.moveTo(centerX, centerY);
            float x = (float) (centerX + radius * Math.cos(angleNow));
            float y = (float) (centerY + radius * Math.sin(angleNow));
            path.lineTo(x, y);
            canvas.drawPath(path, mainPaint);
        }
    }

    /**
     * 绘制文字
     *
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.descent - fontMetrics.ascent;
        for (int i = 0; i < count; i++) {
            angleNow = angle * i - angleInit;
            if (angleNow < 0) {
                angleNow = (float) (Math.PI * 2 + angleNow);
            }
            float x = (float) (centerX + (radius + fontHeight / 2) * Math.cos(angleNow));
            float y = (float) (centerY + (radius + fontHeight / 2) * Math.sin(angleNow));

            if ((Math.ceil(y)) < centerY) {
                y = y - 10;
            } else if ((Math.ceil(y)) > centerY) {
                y = y + 10;
            }

            if ((Math.ceil(x)) < centerX) {
                x = x - 10;
            } else if ((Math.ceil(x)) > centerX) {
                x = x + 10;
            }

            if ((Math.ceil(x) == centerX)) {
                if ((Math.ceil(y)) < centerY) {
                    y = y - 15;
                } else if ((Math.ceil(y)) > centerY) {
                    y = y + 30;
                }
                textPaint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(titles[i], x, y, textPaint);
                continue;
            } else {
                textPaint.setTextAlign(Paint.Align.LEFT);
            }


            if (angleNow >= 0 && angleNow <= Math.PI / 2) {//第4象限
                canvas.drawText(titles[i], x, y, textPaint);
            } else if (angleNow >= 3 * Math.PI / 2 && angleNow <= Math.PI * 2) {//第3象限
                canvas.drawText(titles[i], x, y, textPaint);
            } else if (angleNow > Math.PI / 2 && angleNow <= Math.PI) {//第2象限
                float dis = textPaint.measureText(titles[i]);//文本长度
                canvas.drawText(titles[i], x - dis, y, textPaint);
            } else if (angleNow >= Math.PI && angleNow < 3 * Math.PI / 2) {//第1象限
                try {
                    float dis = textPaint.measureText(titles[i]);//文本长度
                    canvas.drawText(titles[i], x - dis, y, textPaint);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 绘制区域 Two
     *
     * @param canvas
     */
    private void drawRegionOne(Canvas canvas) {
        Path path = new Path();
        valuePaintOne.setAlpha(255);
        for (int i = 0; i < count; i++) {
            angleNow = angle * i - angleInit;
            double percent = data1[i] / maxValue;
            float x = (float) (centerX + radius * Math.cos(angleNow) * percent);
            float y = (float) (centerY + radius * Math.sin(angleNow) * percent);
            if (i == 0) {
                path.moveTo(x, y);
                //path.moveTo(x, centerY);
            } else {
                path.lineTo(x, y);
            }
            //绘制小圆点
            //canvas.drawCircle(x,y,10,valuePaint);
        }
        path.close();
        if (!isLine) {
            valuePaintOne.setAlpha(127);
            //绘制填充区域
            valuePaintOne.setStyle(Paint.Style.FILL_AND_STROKE);
            canvas.drawPath(path, valuePaintOne);
        } else {
            valuePaintOne.setStyle(Paint.Style.STROKE);
            canvas.drawPath(path, valuePaintOne);
        }
    }


    /**
     * 绘制区域 Two
     *
     * @param canvas
     */
    private void drawRegionTwo(Canvas canvas) {
        Path path = new Path();
        valuePaintTwo.setAlpha(255);
        for (int i = 0; i < count; i++) {
            angleNow = angle * i - angleInit;
            double percent = data2[i] / maxValue;
            float x = (float) (centerX + radius * Math.cos(angleNow) * percent);
            float y = (float) (centerY + radius * Math.sin(angleNow) * percent);
            if (i == 0) {
                path.moveTo(x, y);
                //path.moveTo(x, centerY);
            } else {
                path.lineTo(x, y);
            }
            //绘制小圆点
            //canvas.drawCircle(x,y,10,valuePaint);
        }
        path.close();

        if (!isLine) {
            valuePaintTwo.setAlpha(127);
            //绘制填充区域
            valuePaintTwo.setStyle(Paint.Style.FILL_AND_STROKE);
            canvas.drawPath(path, valuePaintTwo);
        } else {
            valuePaintTwo.setStyle(Paint.Style.STROKE);
            canvas.drawPath(path, valuePaintTwo);
        }
    }


    //设置标题
    public void setTitles(String[] titles) {
        this.titles = titles;
    }

    //设置数值 DataOne
    public void setDataOne(double[] data) {
        this.data1 = data;
    }

    //设置数值 DataTwo
    public void setDataTwo(double[] data) {
        this.data2 = data;
    }

    //设置最大数值
    public float getMaxValue() {
        return maxValue;
    }

    //设置最大数值
    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }

    //设置蜘蛛网颜色
    public void setMainPaintColor(int color) {
        mainPaint.setColor(color);
    }

    //设置标题颜色
    public void setTextPaintColor(int color) {
        textPaint.setColor(color);
    }

    //设置覆盖局域颜色
    public void setValueOnePaintColor(int color) {
        valuePaintOne.setColor(color);
    }

    //设置覆盖局域颜色
    public void setValueTwoPaintColor(int color) {
        valuePaintTwo.setColor(color);
    }

    //设置最大层数
    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isLine() {
        return isLine;
    }

    public void setLine(boolean line) {
        isLine = line;
    }

    //设置最大层数
    public void setCount(int count) {
        if (count > 2 && count <= 8) {
            this.count = count;
        } else if (count > 8) {
            this.count = 8;
        }
    }

    public int getCount() {
        return count;
    }

    /**
     * 重绘
     */
    public void resetShow() {
        init();
        invalidate();
    }

}